<template>
	<view>
		<!-- 头部搜索框 -->
		<search />
		<view v-show="this.dataList.length>0">
			<zy-table :headList="headList" :dataList="dataList" border></zy-table>
			<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('homepage.CutMarket.pick')"
				@rightBtn="submitBtn" @leftBtn="resetBtn"></bottomBtn>
		</view>
		<zy-empty v-if="this.dataList.length==0"></zy-empty>
		<!-- 提交确认 -->
		<zy-popup ref="popup" :title="$t('homepage.CutMarket.pickSure')" type="dialog" @confirm="submit">
			{{$t('homepage.CutMarket.isPickSure')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		storageGetEntityByCode,
		outCscWarehouse
	} from "@/api/homepage.js"
	export default {
		data() {
			return {
				//表格表头
				headList: [{
					name: this.$t('homepage.CutMarket.orderNum'),
					key: 'id',
				}, {
					name: this.$t('public.cutCode'),
					key: 'code',
				}],
				dataList: [], //表格数据
				id: 0,
				getStorageCodeId: '',
				barcode: "",
			}
		},
		onLoad() {
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				//清除字符串空格 
				_this.searchList(data.code)
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		methods: {
			searchList(e) {
				storageGetEntityByCode({
					code: e,
				}).then((res) => {
					if (res.code == 0) {
						this.getStorageCodeId = res.data.id
						this.submitBtn()
					} else {
						this.search(e)
					}
				}).catch((res) => {
					setTimeout(() => {
						this.search(e)
					}, 500)
				})
			},
			search(e) {
				if (this.dataList.length == 0) {
					this.id++
					let obj = {}
					obj.id = this.id
					obj.code = e
					this.dataList.push(obj)
				} else {
					if (this.dataList.findIndex(item => item.code === e) !== -1) {
						uni.showToast({
							title: this.$t('public.searhRepeat'),
							icon: "none",
							duration: 3000
						})
					} else {
						this.id++
						let obj = {}
						obj.id = this.id
						obj.code = e
						this.dataList.push(obj)
					}
				}
				console.log(this.dataList)
			},
			// 页面重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/CutMarket/Pick/index"
				})
			},
			//入库提交
			submitBtn() {
				this.$refs.popup.open()
			},
			//弹框确认
			submit() {
				for (var i = 0; i < this.dataList.length; i++) {
					this.barcode += this.dataList[i].code + ','
				}
				outCscWarehouse({
					barcode: this.barcode,
					storageId: this.getStorageCodeId,
				}).then((res) => {
					if (res.code == 0) {
						this.resetBtn()
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>